home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 102_01.zip / WUMPUS.C < prev    next >
Text File  |  1993-06-03  |  9KB  |  390 lines

  1.  
  2. /*
  3.  *    wumpus
  4.  *    borrowed from PCC Vol 2 No 1
  5.  *    Modified for BDS C by Leor Zolman    2/82
  6.  */
  7.  
  8. #define MAXLINE 135
  9. #define    NBAT    3
  10. #define    NROOM    20
  11. #define    NTUNN    3
  12. #define    NPIT    3
  13.  
  14. struct Room
  15. {
  16.     int    tunn[NTUNN];
  17.     int    flag;
  18. } room[NROOM];
  19.  
  20.  
  21. #define    BAT    01
  22. #define    PIT    02
  23. #define    WUMP    04
  24.  
  25. int    arrow;
  26. int    loc;
  27. int    wloc;
  28. int    tchar;
  29.  
  30. main()
  31. {
  32.     register i, j;
  33.     char strbuf[MAXLINE], *cptr;
  34.     register struct Room *p;
  35.     int k, icomp();
  36.  
  37.     printf("\n\nWelcome to \"Hunt the Wumpus\"!\n");
  38.     srand1("Do you need instructions? ");
  39.     if (tolower(getchar()) == 'y')
  40.         instructs();
  41.  
  42. /*
  43.  * initialize the room connections
  44.  */
  45.  
  46. init:
  47.     p = &room[0];
  48.     for(i=0; i<NROOM; i++) {
  49.         for(j=0; j<NTUNN; j++)
  50.             p->tunn[j] = -1;
  51.         p++;
  52.     }
  53.     k = 0;
  54.     for(i=1; i<NROOM; ) {
  55.         j = rnum(NROOM);
  56.         p = &room[j];
  57.         if(j == k || p->tunn[0] >= 0 || p->tunn[1] >= 0)
  58.             continue;
  59.         p->tunn[1] = k;
  60.         room[k].tunn[0] = j;
  61.         k = j;
  62.         i++;
  63.     }
  64.     p = &room[0];
  65.     for(i=0; i<NROOM; i++) {
  66.         for(j=0; j<NTUNN; j++) {
  67.             if(p->tunn[j] < 0)
  68.                 p->tunn[j] = tunnel(i);
  69.             if(p->tunn[j] == i)
  70.                 goto init;
  71.             for(k=0; k<j; k++)
  72.                 if(p->tunn[j] == p->tunn[k])
  73.                     goto init;
  74.         }
  75.         qsort(&p->tunn[0], NTUNN, 2, icomp);
  76.         p++;
  77.     }
  78.  
  79. /*
  80.  * put in player, wumpus,
  81.  * pits and bats
  82.  */
  83.  
  84. setup:
  85.     arrow = 5;
  86.     p = &room[0];
  87.     for(i=0; i<NROOM; i++) {
  88.         p->flag = 0;
  89.         p++;
  90.     }
  91.     for(i=0; i<NPIT; ) {
  92.         p = &room[rnum(NROOM)];
  93.         if((p->flag&PIT) == 0) {
  94.             p->flag |= PIT;
  95.             i++;
  96.         }
  97.     }
  98.     for(i=0; i<NBAT; ) {
  99.         p = &room[rnum(NROOM)];
  100.         if((p->flag&(PIT|BAT)) == 0) {
  101.             p->flag |= BAT;
  102.             i++;
  103.         }
  104.     }
  105.     i = rnum(NROOM);
  106.     wloc = i;
  107.     room[i].flag |= WUMP;
  108.     for(;;) {
  109.         i = rnum(NROOM);
  110.         if((room[i].flag&(PIT|BAT|WUMP)) == 0) {
  111.             loc = i;
  112.             break;
  113.         }
  114.     }
  115.     putchar('\n');
  116.  
  117. /*
  118.  *    main loop of the game
  119.  */
  120.  
  121. loop:
  122.     printf("\nYou are in room %d\n", loc+1);
  123.     p = &room[loc];
  124.     if(p->flag&PIT) {
  125.        printf("You fell into a pit and broke every bone in your body.\n");
  126.         goto done;
  127.     }
  128.     if(p->flag&WUMP) {
  129.         printf("You were eaten by the wumpus. Yum yum. Belch.\n");
  130.         goto done;
  131.     }
  132.     if(p->flag&BAT) {
  133.        printf("Theres a bat in your room...it picks you up, up, up...\n");
  134.         loc = rnum(NROOM);
  135.         goto loop;
  136.     }
  137.     for(i=0; i<NTUNN; i++)
  138.     if(near(&room[p->tunn[i]], WUMP))
  139.         goto nearwump;
  140.     if (near(p, WUMP)) {
  141.     nearwump:
  142.         printf("I smell a wumpus\n");
  143.     }
  144.     if (near(p, BAT))
  145.         printf("Bats nearby\n");
  146.     if (near(p, PIT))
  147.         printf("I feel a draft\n");
  148.  
  149. again:
  150.     printf("There are tunnels to");
  151.     for(i=0; i<NTUNN; i++)
  152.         printf(" %d", p->tunn[i]+1);
  153.     printf("\n");
  154.  
  155.     printf("Move or shoot (m-s) ?");
  156.     gets(strbuf);
  157.  
  158.     for (i = 0; strbuf[i]; i++) {
  159.         strbuf[i] = tolower(strbuf[i]);
  160.         if (strbuf[i] == ',') strbuf[i] = ' ';
  161.     }
  162.  
  163.     for (i = 0; isspace(strbuf[i]); i++)
  164.         ;
  165.  
  166.     if (strbuf[i] == 'm') {
  167.         i++;
  168.         if (!isdigit(ignore_s(&strbuf[i]))) {
  169.             printf("Move to which room? ");
  170.             gets(strbuf);
  171.             if (!isdigit(ignore_s(strbuf))) {
  172.                 printf("I don't understand, so I'll ");
  173.                 printf("leave you where you were.\n");
  174.                 goto again;
  175.             }
  176.             i = atoi(strbuf) - 1;
  177.         }
  178.         else i = atoi(&strbuf[i]) - 1;
  179.             for(j=0; j<NTUNN; j++)
  180.             if(i == p->tunn[j])
  181.                 goto groom;
  182.         printf("You hit the wall\n");
  183.         goto again;
  184.     groom:
  185.         loc = i;
  186.         if(i == wloc)
  187.             goto mwump;
  188.         goto loop;
  189.     }
  190.  
  191.     if (strbuf[i] != 's') {
  192.         printf("I don't understand that.\n");
  193.         goto again;
  194.     }
  195.  
  196.     cptr = &strbuf[i + 1];
  197.     
  198.     if (!isdigit(ignore_s(cptr))) {
  199.         printf("Type a list of up to five rooms to shoot through: ");
  200.         gets(strbuf);
  201.         if (!isdigit(ignore_s(strbuf))) {
  202.             printf("I don't understand, so I assume you don't ");
  203.             printf("want to shoot.\n");
  204.             goto again;
  205.         }
  206.         cptr = strbuf;
  207.     }
  208.  
  209.     for(i=0; i<5; i++) {
  210.         if (!isdigit(ignore_s(cptr)))
  211.             break;
  212.         j = atoi(cptr) - 1;
  213.         if (j == -1)
  214.             break;
  215.         while (isspace(*cptr)) cptr++;
  216.         while (isdigit(*cptr)) cptr++;
  217.  
  218.     ranarw:
  219.         for(k=0; k<NTUNN; k++)
  220.             if(j == p->tunn[k])
  221.                 goto garow;
  222.         j = rnum(NROOM);
  223.         goto ranarw;
  224.     garow:
  225.         p = &room[j];
  226.         if(j == loc) {
  227.             printf("You shot yourself. You lose.\n");
  228.             goto done;
  229.         }
  230.         if(p->flag&WUMP) {
  231.             printf("\7You slew the wumpus!\n");
  232.             goto done;
  233.         }
  234.     }
  235.     if(--arrow == 0) {
  236.         printf("That was your last shot...you lose.\n");
  237.         goto done;
  238.     }
  239.     goto mwump;
  240.  
  241. mwump:
  242.     p = &room[wloc];
  243.     p->flag &= ~WUMP;
  244.     i = rnum(NTUNN+1);
  245.     if(i != NTUNN)
  246.         wloc = p->tunn[i];
  247.     room[wloc].flag |= WUMP;
  248.     goto loop;
  249.  
  250. done:
  251.     printf("\n\nAnother game? (y-n) ");
  252.     if(tolower(getchar()) == 'y') {
  253.         printf("\nSame room setup? (y-n) ");
  254.         if(tolower(getchar()) == 'y')
  255.             goto setup;
  256.         goto init;
  257.     }
  258. }
  259.  
  260. char ignore_s(s)
  261. char *s;
  262. {
  263.     while (isspace(*s)) s++;
  264.     return *s;
  265. }
  266.     
  267. tunnel(i)
  268. {
  269.     register struct Room *p;
  270.     register n, j;
  271.     int c;
  272.  
  273.     c = 20;
  274.  
  275. loop:
  276.     n = rnum(NROOM);
  277.     if(n == i)
  278.         if(--c > 0)
  279.             goto loop;
  280.     p = &room[n];
  281.     for(j=0; j<NTUNN; j++)
  282.     if(p->tunn[j] == -1) {
  283.         p->tunn[j] = i;
  284.         return(n);
  285.     }
  286.     goto loop;
  287. }
  288.  
  289. rnum(n)
  290. {
  291.     rand() % n;
  292. }
  293.  
  294. near(ap, ahaz)
  295. struct Room *ap;
  296. {
  297.     register struct Room *p;
  298.     register haz, i;
  299.  
  300.     p = ap;
  301.     haz = ahaz;
  302.     for(i=0; i<NTUNN; i++)
  303.     if(room[p->tunn[i]].flag & haz)
  304.         return (1);
  305.     return(0);
  306. }
  307.  
  308. icomp(p1, p2)
  309. int *p1, *p2;
  310. {
  311.  
  312.     return(*p1 - *p2);
  313. }
  314.  
  315. instructs()
  316. {
  317.     printf("\n");
  318.     printf("Welcome to 'Hunt the Wumpus.'\n");
  319.     printf("\n");
  320.     printf("The Wumpus lives in a cave of %d rooms.\n",NROOM);
  321.     printf("Each room has %d tunnels leading to other rooms.\n",NTUNN);
  322.     printf("\n");
  323.     printf("Hazards:\n");
  324.     printf("\n");
  325.     printf("Bottomless Pits - Some rooms have Bottomless Pits in them.\n");
  326.     printf("    If you go there, you fall into the pit and lose!\n");
  327.     printf("Super Bats - Some other rooms have super bats.\n");
  328.     printf("    If you go there, a bat will grab you and take you\n");
  329.     printf("    to somewhere else in the cave where you could\n");
  330.     printf("    fall into a pit or run into the . . .\n");
  331.  
  332.     printf("\n(Hit any character to continue) "); getchar();
  333.  
  334.     printf("\n\n. . .Wumpus:\n");
  335.     printf("\n");
  336.     printf("The Wumpus is not bothered by the hazards since\n");
  337.     printf("he has sucker feet and is too big for a bat to lift.\n");
  338.     printf("\n");
  339.     printf("Usually he is asleep.\n");
  340.     printf("Two things wake him up:\n");
  341.     printf("    your entering his room\n");
  342.     printf("    your shooting an arrow anywhere in the cave.\n");
  343.     printf("If the wumpus wakes, he either decides to move one room or\n");
  344.     printf("stay where he was.  But if he ends up where you are,\n");
  345.     printf("he eats you up and you lose!\n");
  346.     printf("You:\n");
  347.     printf("\n");
  348.     printf("Each turn you may either move or shoot a crooked arrow.\n");
  349.     printf("\n");
  350.     printf("Moving - You can move to one of the adjoining rooms;\n");
  351.     printf("    that is, to one that has a tunnel connecting it\n");
  352.     printf("    with the room you are in.\n");
  353.  
  354.     printf("\n(Hit any character to continue) "); getchar();
  355.     printf("\n\n");
  356.     printf("Shooting - You have 5 arrows.  You lose when you run out.\n");
  357.     printf("    Each arrow can go from 1 to 5 rooms.\n");
  358.     printf("    You aim by giving a list of room numbers\n");
  359.     printf("    telling the arrow which rooms to go through.\n");
  360.     printf("    The first room in the path must be connected to \n");
  361.     printf("    the room you are in.  Each succeeding room must be\n");
  362.     printf("    connected to the previous room.\n");
  363.     printf("    If there is no tunnel between two of the rooms\n");
  364.     printf("    in the arrow's path, the arrow chooses one of the\n");
  365.     printf("    three tunnels from the room it's in and goes its\n");
  366.     printf("    own way.\n");
  367.     printf("\n");
  368.     printf("    If the arrow hits the wumpus, you win!\n");
  369.     printf("    If the arrow hits you, you lose!\n");
  370.  
  371.     printf("\n(Hit any character to continue) "); getchar();
  372.  
  373.     printf("\n\nWarnings:\n");
  374.     printf("\n");
  375.     printf("When you are one or two rooms away from the wumpus,\n");
  376.     printf("the computer says:\n");
  377.     printf("        'I smell a Wumpus'\n");
  378.     printf("When you are one room away from some other hazard, it says:");
  379.     printf("\n        Bat    - 'Bats nearby'\n");
  380.     printf("        Pit    - 'I feel a draft'\n");
  381.     printf("\n");
  382.     printf("Giving commands:\n");
  383.     printf("    When asked to move or shoot by typing 'm' or 's',\n");
  384.     printf("    you can give the required room number(s) on the\n");
  385.     printf("    same line. If you don't,  the computer will ask\n");
  386.     printf("    you for them on the next line.\n\n");
  387.     printf("G O O D   L U C K   !!!!!!\n");
  388. }
  389.  
  390.